package com.henry.customviewstudy.app;

import android.app.Activity;
import android.app.Application;

import java.util.ArrayList;
import java.util.logging.Handler;

/**
 * Created by henry on 2018/4/5.
 */

public class ViewApplication extends Application {
    public static ViewApplication viewApplication;
    //用于存放Activity的list
    private ArrayList<Activity> activities;
    public static ViewApplication getInstance() {
        if (viewApplication == null) {
            synchronized (ViewApplication.class) {
                if (viewApplication == null) {
                    viewApplication = new ViewApplication();
                }
            }
        }
        return viewApplication;
    }
    private static int applicationThreadId;
    private static Handler uiHandler;

    @Override
    public void onCreate() {
        super.onCreate();
        viewApplication =this;
    }

    public static int getApplicationThreadId() {
        return applicationThreadId;
    }

    public static Handler getUiHandler() {
        return uiHandler;
    }
    //最多保留四个Activity实例对象
    public void deleteOtherActivity(Activity activity){
        if (activities == null){
            activities = new ArrayList<>();
        }
        activities.add(activity);
        if (activities.size()>4){
            activities.get(0).finish();
            activities.remove(0);
        }
    }
    //用于清除Activitys
    public void cleanActivities(){
        if (activities!=null){
            activities.clear();
            activities =null;
        }
    }
    /**
     * 把当前Activity从activities中移除
     */
    public void removeActivity(){
        if (activities!=null&&activities.size()>0){
            activities.remove(activities.size()-1);
        }
    }
}
